package org.jeecg.modules.paycenter.channel.zxbank.utils;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.HttpTools;
import org.jeecg.common.util.XmlUtils;
import org.jeecg.modules.paycenter.channel.zxbank.dto.BasePacket;
import org.jeecg.modules.paycenter.channel.zxbank.dto.BaseUserData;
import org.jeecg.modules.paycenter.channel.zxbank.dto.RetPacket;
import org.jeecg.modules.paycenter.channel.zxbank.dto.UserDataList;
import org.jeecg.modules.paycenter.channel.zxbank.entity.ZxAccountInfo;
import org.jeecg.modules.paycenter.exception.BankSendException;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.net.ConnectException;
import java.util.Collection;

public class ZXUtil {
    private static Logger logger = LoggerFactory.getLogger("bankLog");
    public static RetPacket sendData(BasePacket packet){
        HyConfig config = HyConfigUtils.getConfig();
        String username = config.getZxUserName();
        String url = config.getZxUrl();
//        String url = "http://117.78.0.170:6789";
        if(StringUtils.isEmpty(username)){
            throw new JeecgBootException("中信银企通username不能为空");
        }
        if(StringUtils.isEmpty(url)){
            throw new JeecgBootException("中信银企通url不能为空");
        }

        packet.setUserName(username);//登录名
        String data = "";
        try {
            data = XmlUtils.converTomXml(packet);
            String content = null;
            data = "<?xml version=\"1.0\" encoding=\"gbk\"?>" + data;
            logger.info("发送数据：" + data);
            content = HttpTools.postXml(url, data, "gbk");
            logger.info("接收数据：" + content);
            RetPacket retPacket = XmlUtils.converyToJavaBean(content, RetPacket.class);
            return retPacket;
        }catch (ConnectException e){
            throw new BankSendException("交易失败：发起交易请求超时");
        }
        catch (Exception e){
            throw new JeecgBootException("交易失败：" + e.getMessage());
        }
    }
    //单笔支付
    public static RetPacket exchange(String clientID,
                              String dbAccNo,/* String dbAccName,*/
                              String crAccNo ,String crAccName ,
                              BigDecimal amt , String bankCode, String postscript){
        BasePacket packet = new BasePacket();
        packet.setAction("DLUPBSUB");
        packet.setClientID(clientID);//客户流水号
        packet.setPayAccountNo(dbAccNo);//付款账号
        packet.setTotalNumber(1);
        packet.setTotalAmount(amt);
        packet.setAbstract(postscript);
        packet.setChkNum("");

        UserDataList list = new UserDataList();

        BaseUserData row = new BaseUserData();
        row.setID(1);
        row.setRecAccountName(crAccName);
        row.setRecAccountNo(crAccNo);
        row.setRecBankCode(bankCode);
        row.setTranAmount(amt);
        row.setAbstract(postscript);
        list.addData(row);

        packet.setList(list);
        return sendData(packet);
    }

    //查询交易状态
    public static RetPacket getExchangeStatus(String clientID){
        BasePacket packet = new BasePacket();
        packet.setAction("DLUPBSTT");
        packet.setClientID(clientID);//客户流水号
        packet.setID("");
        return sendData(packet);
    }

    //查询账户余额
    public static  ZxAccountInfo getAccountBalance(String accNo){
        try {
            BasePacket packet = new BasePacket();
            packet.setAction("DLBALQRY");
            UserDataList list = new UserDataList();
            BaseUserData row = new BaseUserData();
            row.setID(1);
            row.setAccountNo(accNo);
            list.addData(row);

            packet.setList(list);
            RetPacket retPacket = sendData(packet);
            if (retPacket.isSuccess()) {
                BaseUserData userData = retPacket.getList().getRow().get(0);
                ZxAccountInfo acc = new ZxAccountInfo();
                BeanUtils.copyProperties(userData, acc);
                return acc;
            }
        }catch (Exception e){

        }
        return null;
    }

    //查询电子回单
    public static RetPacket getHd(String accNo , String payDate , BigDecimal minAmt ,  BigDecimal maxAmt ){
        BasePacket packet = new BasePacket();
        packet.setAction("DLEDDRSQ");
        packet.setQryType("1");
        packet.setAccNo(accNo);
        packet.setBillType("1");
//        packet.setMinAmt(BigDecimal.ZERO);
        packet.setMinAmt(minAmt);
        packet.setMaxAmt(maxAmt);
        packet.setStartDate(payDate);
        packet.setEndDate(payDate);
        packet.setPageSize(100);
        packet.setStartNo(1);
        return sendData(packet);
    }

    //查询电子回单
//    public static RetPacket downloadHd(String accNo , List<String> brSeqNos){
    public static RetPacket downloadHd(String accNo , Collection<?>  brSeqNos){
        BasePacket packet = new BasePacket();
        packet.setAction("DLEDCDTD");
        packet.setAccNo(accNo);
        packet.setBankId("");
        packet.setIsCurrDay("1");

        UserDataList list = new UserDataList();
        for (Object brSeqNo : brSeqNos) {
            String brSeqNo1 = (String) brSeqNo;
            BaseUserData row = new BaseUserData();
            row.setBrSeqNo(brSeqNo1);
            row.setBillType("200000");
            list.addData(row);
        }
        packet.setList(list);
        return sendData(packet);
    }
//    public static RetPacket getBankCode(String accno){
//        BasePacket packet = new BasePacket();
//        packet.setAction("DLBNKCOD");
//        packet.setTgfi(accno);
//        return sendData(packet);
//    }

//    public  static RetPacket getBankHd(){
//        BasePacket packet = new BasePacket();
//        packet.setAction("DLUPBSTT");
//    }

    public static void main(String[] args) {

        HyConfig config = new HyConfig();
        config.setZxIzUse(true);
        config.setZxUserName("11100110355698290619");
        config.setZxAccNo("8115501012600225824");
        config.setZxUrl("http://117.78.0.170:6789");
//        config.setZxUrl("123");
        config.setZxAccName("太原银企测试一");
        config.setZxBankCode("302161026112");
        config.setZxKhh("中信银行太原分行营业部");
//        HyConfigUtils.setConfig(config);


        RetPacket packet = null;
//        packet = ZXUtil.exchange("129", "8115501012600225824","6217690700022480", "滕度正", new BigDecimal("12.3"), "302100011106", "运费");
//        packet = ZXUtil.exchange("130", "8115501012600225824","6228480010262653222", "账单一", new BigDecimal("12.3"), "", "运费");
//        packet =ZXUtil.getExchangeStatus("130");

//        ZxAccountInfo accountBalance = ZXUtil.getAccountBalance("8115501012600225824");
//        System.out.println(accountBalance);

        packet = ZXUtil.getHd("8115501012600225824","20220318" , new BigDecimal("1000"),new BigDecimal("3000"));
//        System.out.println(packet);

//        ZXUtil.downloadHd("8115501012600225824","20220317SC250001755097000001");

//        String s = FileUtil.readString("d://1.txt", "utf-8");
//        byte[] decode = Base64.getDecoder().decode(s);
//        FileUtil.writeBytes(decode,"d://URCP_001_HDCOLB_a98a227e7c5f49e884d6293f196b96c0_20220317.zip");

//        System.out.println(packet);
//        if(packet.isSuccess()){
//            UserData userData = packet.getList().getRow().get(0);
//            System.out.println(userData.getInfo());
//            if(userData.isSTTSuccess()){
//                System.out.println("交易成功！！！");
//            }else {
//                if( userData.isSTTFail()){
//                    System.out.println("交易失败！！！");
//                }else {
//                    System.out.println("请稍后查询！！！");
//                }
//            }
//        }else {
//            System.out.println("查询交易失败");
//        }

    }
}
